package com.lzw.service.impl.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lzw.mapper.admin.DormitoryManagementMapper;
import com.lzw.mapper.admin.StudentManagementMapper;
import com.lzw.pojo.Dormitory;
import com.lzw.service.admin.DormitoryManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DormitoryManagementServiceImpl implements DormitoryManagementService {

    @Autowired
    private DormitoryManagementMapper dormitoryManagementMapper;

    @Autowired
    private StudentManagementMapper studentManagementMapper;

    @Override
    public List<Dormitory> listDormitory(Integer pageNum, Integer pageSize, Integer dormitoryId, Integer buildingId) {
        PageHelper.startPage(pageNum,pageSize);
        List<Dormitory> list = dormitoryManagementMapper.listDormitory(dormitoryId,buildingId);
        Page<Dormitory> dormitories = (Page<Dormitory>) list;
        return dormitories;
    }

    @Override
    public void addDormitory(Dormitory dormitory) {
        //寝室号已存在
        if (dormitoryManagementMapper.countById(dormitory.getDormitoryId()) > 0){
            throw new IllegalArgumentException("寝室号已存在");
        }
        dormitoryManagementMapper.addDormitory(dormitory);
    }

    @Override
    public void deleteDormitory(List<Integer> ids) {
        dormitoryManagementMapper.deleteDormitory(ids);
        studentManagementMapper.deleteByDormitoryId(ids);
    }

    @Override
    public Dormitory getDormitoryById(int id) {
        return dormitoryManagementMapper.getDormitoryById(id);
    }
}
